package com.controller.zk;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.bean.zk.Role;
import com.service.zk.RoleService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
public class RoleController {

    @Resource
    private RoleService roleService;

    /**
     * 返回所有用户角色
     */
    @RequestMapping("/page/getroles")
    @ResponseBody
    public List<Map<String,Object>> get(){
        return roleService.getRoles();
    }

    /**
     * 返回当前用户角色
     */
    @RequestMapping("/page/getrole/{eid}")
    @ResponseBody
    public List<Map<String,Object>> get2(@PathVariable String eid){
        return roleService.getRolesByEid(eid);
    }

    /**
     * 添加角色
     */
    @RequestMapping("/page/addRoleById")
    @ResponseBody
    public int add(String eid ,@RequestParam("ids") Object ids){
        JSONArray idsarray = JSON.parseArray(ids.toString());
        Object[] objects = idsarray.toArray();
        return roleService.addRoleById(eid,objects);
    }

    /**
     * 删除角色
     */
    @RequestMapping("/page/removeRoleById")
    @ResponseBody
    public int remove(String eid ,@RequestParam("ids") Object ids){
        JSONArray idsarray = JSON.parseArray(ids.toString());
        Object[] objects = idsarray.toArray();
        return roleService.removeRoleById(eid,objects);
    }

    /**
     * 删除角色
     */
    @RequestMapping("/page/delrole/{rid}")
    @ResponseBody
    public int remove(@PathVariable String rid , HttpServletRequest request){
        return roleService.delRoleById(rid,request);
    }


    /**
     * 查询角色
     */
    @RequestMapping("/page/getroleByRid/{rid}")
    @ResponseBody
    public Role select(@PathVariable String rid){
        return roleService.getRole(rid);
    }
    /**
     * 修改角色
     */
    @RequestMapping("/page/updateroleByRid/{rid}")
    @ResponseBody
    public int select(@PathVariable String rid,Role role, HttpServletRequest request){
        role.setRid(Integer.parseInt(rid));
        return roleService.updateRoleByRid(role,request);
    }
    /**
     * 添加角色
     */
    @RequestMapping("/page/addRole")
    @ResponseBody
    public int select(String rname,String remark, HttpServletRequest request){
        return roleService.addRole(rname,remark,request);
    }


}
